package com.hong.qrpc.listener.impl;

import com.hong.qrpc.RemoteInvoke;
import com.hong.qrpc.RpcClient;
import com.hong.qrpc.annotation.QRpcClient;
import com.hong.qrpc.listener.Listener;
import com.hong.qrpc.listener.RpcClientListener;

import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/7/23 20:10
 */
public class Registrar implements RpcClientListener {
    private String helpCompare = "com.hong.qrpc.listener.impl.Registrar";
    private List<RemoteInvoke> remoteInvokes;

    public Registrar(List<RemoteInvoke> remoteInvokes) {
        this.remoteInvokes = remoteInvokes;
    }

    /**
     * 先去获取注册信息，并且将所有的服务信息存在本地的服务集合中，然后进行包扫描，为所有指定的参数注入其代理对象
     *
     * @param rpcClient
     */
    @Override
    public void afterRpcClientInit(RpcClient rpcClient) {
        for (RemoteInvoke remoteInvoke : remoteInvokes) {
            QRpcClient anno = (QRpcClient) remoteInvoke.getInterfaceClass().getAnnotation(QRpcClient.class);
            rpcClient.addSelectorIfAbsent(anno.serviceName());
            rpcClient.fullRemoteInvoker(remoteInvoke);
        }
    }
//    /**
//     * 递归地去寻找需要加载的服务实例对象
//     *
//     * @param curFile 方便获取路径和进行递归
//     */
//    private void registerFromPackage(File curFile, Deque<String> subUrls) {
//        if (curFile.isFile()) {
//            String curPath = curFile.getPath();
//            if (curPath.endsWith(".class")) {
//                try {
//                    Class clazz = Class.forName(StringUtils.getClassName(subUrls));
//                    //需要检查成员变量和字段是否需要注入
//                    for (Method method : clazz.getDeclaredMethods()) {
//                        Reference anno = null;
//                        if ((anno = method.getAnnotation(Reference.class)) != null) {
//                            ReflectionUtils.invoke()
//                        }
//                    }
//                } catch (ClassNotFoundException e) {
//                    e.printStackTrace();
//                }
//            }
//        } else {
//            for (String subUrl : curFile.list()) {
//                subUrls.addLast(subUrl);
//                registerFromPackage(new File(curFile, subUrl), subUrls);
//                subUrls.removeLast();
//            }
//        }
//    }

    public static void addRegistrar(RpcClient rpcClient, List<RemoteInvoke> remoteInvokes) {
        Map<Class<? extends Listener>, HashSet<Listener>> listenerSets = rpcClient.getListenerManager().getListenerSets();
        HashSet<Listener> listeners = listenerSets.get(RpcClientListener.class);
        if (listeners == null) {
            listeners = new HashSet<>();
            listenerSets.put(RpcClientListener.class, listeners);
        }
        Registrar rtr = new Registrar(remoteInvokes);
        if (!listeners.contains(rtr)) {
            listeners.add(rtr);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Registrar registrar = (Registrar) o;

        return helpCompare.equals(registrar.helpCompare);
    }

    @Override
    public int hashCode() {
        return helpCompare.hashCode();
    }
}
